#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <pthread.h>
#include <sys/stat.h>

void read_dir(char *dir, void (*func)(char *));
void isFile(char *name)
{
    int ret = 0;
    struct stat sb;
    ret = stat(name, &sb);
    if(ret == -1){
        perror("stat error");
        return;
    }
    if(S_ISDIR(sb.st_mode)){
        read_dir(name, isFile);
    }
    printf("%s\t%ld\n", name, sb.st_size);
}

void read_dir(char *dir, void(*func)(char *))
{
    char path[256];
    DIR *dp;
    struct dirent *sdp;

    dp = opendir(dir);
    if(dp == NULL){
        perror("opendir error");
        return;
    }
    while((sdp = readdir(dp))!=NULL){
        if (strcmp(sdp->d_name, ".") == 0 || strcmp(sdp->d_name, "..") == 0)
            continue;
        sprintf(path, "%s/%s", dir, sdp->d_name);
        (*func)(path);
    }
}

int main(int argc, char const *argv[])
{
    if(argc == 1){
        isFile(".");
    }else{
        char *path = (char*)argv[1];
        isFile(path);
    }
    return 0;
}
